import React, { Component, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { withRouter, Link } from 'react-router-dom';
import axios from 'axios';
import Switch from 'rc-switch';
import { GLOBAL_SETTINGS } from '../../containers/SettingPage/settingReducer';
import './styles.scss';
import Button from '../Button';
import Modal from '../Modal';
import ListItems from '../ListItems';
import UserAvatar from '../UserAvatar';
import GroupAvatar from '../GroupAvatar';
import Header from '../../containers/Header';

function openRepoUrl(history) {
  if (process.env.NODE_ENV === 'production') {
    history.push('/group_chat/ddbffd80-3663-11e9-a580-d119b23ef62e');
  } else {
    window.open('https://im.aermin.top/group_chat/ddbffd80-3663-11e9-a580-d119b23ef62e');
  }
}

class List extends Component {
  constructor(props) {
    super(props);
    this.state = {
      homePageList: [{ name: '1' }],
      allGroupChats: [],
      dataList: [
        { name: 'cccc', message: 'ccc' },
        { name: 'aaaa', message: 'ccc' },
        { name: 'kkk', message: 'ccc' },
      ],
    };
  }

  clickItemHandle = () => {
    console.log('');
  };

  searchFieldChange = () => {};

  render() {
    const { homePageList, allGroupChats, dataList } = this.state;

    const listItems = 
      dataList &&
      dataList.map((data, index) => {
        let message = data.message;
        const isShareUrl = message && /::share::{"/.test(message);                         
        if (isShareUrl) {
          message = '[邀请卡片]';
        }
        const chatFromId = data.to_group_id || (data.user_id && data.user_id.toString());
        const isGroupChat = !!data.to_group_id;
        let GroupMembers;
        if (isGroupChat) {
          const chatItem = allGroupChats && allGroupChats.get(data.to_group_id);
          GroupMembers = chatItem && chatItem.groupInfo && chatItem.groupInfo.members;
        }
        // const { params } = match;
        const unreadColor = data.to_group_id ? 'groupUnread' : 'privateUnread';
        let unreadCircular;
        switch (data.unread && data.unread.toString().length) {
          case 2:
            unreadCircular = 'twoDigitsUnread';
            break;
          case 3:
            unreadCircular = 'threeDigitsUnread';
            break;
          default:
            unreadCircular = 'oneDigitUnread';
        }
        const user_id = '1';
        return (
          <Link key={index} to={`/private_chat/${user_id}`}>
            <li value={chatFromId}>
              {isGroupChat ? (
                <GroupAvatar members={GroupMembers || []} />
              ) : (
                <UserAvatar
                  src={data.avatar}
                  name={data.name}
                  size="46"
                  showLogo={!!data.github_id}
                />
              )}

              {!!data.unread && !showAsContacts && (
                <span className={classnames(unreadColor, unreadCircular)}>
                  {data.unread > 99 ? '99+' : data.unread}
                </span>
              )}

              <div className="content">
                <div className="title">
                  <p className="name">{data.name}</p>
                  <span className="time">{!!data.time && toNormalTime(data.time)}</span>
                </div>

                <div className="message">
                  {data.showCallMeTip && <span className="callMe">[有人@我]</span>}
                  {message || '暂无消息'}
                </div>
              </div>
            </li>
          </Link>
        );
      });
    return (
      <div className="home-page-list-wrapper">
        <Header searchFieldChange={field => this.searchFieldChange(field)} />
        <div className="home-page-list-content">
          <ul className="homePageList">{listItems}</ul>
        </div>
      </div>
    );
  }
}

List.propTypes = {
  initApp: PropTypes.func,
  history: PropTypes.object.isRequired,
  globalSettings: PropTypes.object,
  setGlobalSettings: PropTypes.func,
};

List.defaultProps = {
  initApp() {},
  globalSettings: {},
  setGlobalSettings() {},
};

export default withRouter(List);
